package com.qf.smartdevice.controller;

import com.github.pagehelper.PageInfo;
import com.qf.smartdevice.dot.Result;
import com.qf.smartdevice.pojo.SysCategory;
import com.qf.smartdevice.service.SysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王博康
 * @version 1.0
 * @project devicemanagementsystem
 * @description: TODO
 * @date 2022/3/20 11:25 星期日
 */
@RestController
@RequestMapping("/category")
public class SysCategoryController {

    private SysCategoryService sysCategoryService;

    @Autowired
    public void setSysCategoryService(SysCategoryService sysCategoryService) {
        this.sysCategoryService = sysCategoryService;
    }

    @PostMapping("/category")
    public Result addCategory(@RequestBody SysCategory sysCategory){
        sysCategoryService.addCategory(sysCategory);
        return Result.setOk();
    }

    @GetMapping("/category/{id}")
    public Result findById(@PathVariable Long id){
        SysCategory sysCategory = sysCategoryService.findById(id);
        return Result.setOk(sysCategory);
    }

    @GetMapping("/categories")
    public Result findByPage(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "1")int pageSize,String categoryName){
        PageInfo<SysCategory> pageInfo = sysCategoryService.findByPage(pageNum, pageSize, categoryName);
        return Result.setOk(pageInfo);
    }

    @PostMapping("/category/edit")
    public Result updateCategory(@RequestBody SysCategory sysCategory){
        int count=sysCategoryService.updateCategory(sysCategory);
        return Result.setOk(count);
    }

    @PostMapping("/category/delete")
    public Result deleteByIds(@RequestBody List<Long> ids){
        int count=sysCategoryService.deleteByIds(ids);
        return Result.setOk(count);
    }
}
